package com.shane.components.comboBox
{
	import com.EventWithParams.EventWithParams;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	public class comboBox extends MovieClip
	{
		private var selected:Boolean = false;
		private var dropDownList:Vector.<MovieClip> = new Vector.<MovieClip>;
		private var dropDnItem:dropDownItem;
		private var removedCount:int;
		private var alreadyDn:Boolean = false;
		private var highlightedDDItem:int = -1;
		private var stopOnChange:Boolean = false;
		public function comboBox()
		{
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
		private function init(e:Event) :void {
			darkness.alpha = 0;
			darkness.addEventListener(MouseEvent.ROLL_OVER, MOver, false, 0, true);
			darkness.addEventListener(MouseEvent.CLICK, MClick, false, 0, true);
			darkness.addEventListener(MouseEvent.ROLL_OUT, MOut, false, 0, true);
			txt.addEventListener(FocusEvent.FOCUS_IN, Tselect, false, 0, true);
			txt.addEventListener(FocusEvent.FOCUS_OUT, Tdeselect, false, 0, true);
			txt.addEventListener(Event.CHANGE, Tchange, false, 0, true);
			darkness.buttonMode = true;
			txt.mouseEnabled = false;
			txt.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
			txt.addEventListener(KeyboardEvent.KEY_DOWN,keyDnListener);
		}
		private function keyDnListener(e:KeyboardEvent):void {
			trace("keyDownHandler: " + e.keyCode);
            trace("ctrlKey: " + e.ctrlKey);
            trace("keyLocation: " + e.keyLocation);
            trace("shiftKey: " + e.shiftKey);
            trace("altKey: " + e.altKey);
			
			if (e.keyCode == 40  && !alreadyDn) { //up
				if(dropDownList.length >= highlightedDDItem && highlightedDDItem != -1) {dropDownList[highlightedDDItem].darkness.visible = false};
				highlightedDDItem++;
				if(highlightedDDItem > dropDownList.length -1) {highlightedDDItem = 0};
				dropDownList[highlightedDDItem].darkness.visible = true;
				alreadyDn = true;
				trace("down");
			} else if (e.keyCode == 38 && !alreadyDn ) { //dn
				if(dropDownList.length >= highlightedDDItem && highlightedDDItem != -1) {dropDownList[highlightedDDItem].darkness.visible = false};
				highlightedDDItem--;
				if(highlightedDDItem <  0) {highlightedDDItem = dropDownList.length-1};
				dropDownList[highlightedDDItem].darkness.visible = true;
				alreadyDn = true;
			}  else if (e.keyCode == 13  && !alreadyDn ) { //enter
				if(highlightedDDItem != -1) {
				stopOnChange=true;
				clearDD();
				txt.text = dropDownList[highlightedDDItem].txt.text;
				stage.focus = null;
				dispatchEvent( new Event ("NEW_SELECTION"));
				highlightedDDItem = -1;
				//dropDownList[highlightedDDItem].dispatchEvent( new MouseEvent(MouseEvent.CLICK, false, false, 0,0, dropDownList[highlightedDDItem]));
				} else {
					dispatchEvent( new Event ("NEW_SELECTION"));
				}
			}  else if (e.keyCode == 8  && !alreadyDn ) { //del
				txt.text = String(txt.text).substr(0, txt.text.length - 1);
				this.Tchange();
			} else {
				trace(String.fromCharCode(e.charCode));
				txt.appendText(String.fromCharCode(e.charCode));
				this.Tchange();
			}
		}
		private function keyUpListener(e:KeyboardEvent):void {
			alreadyDn = false;
		}
		private function MClick (e:MouseEvent) {
			//txt.mouseEnabled = true;
			stage.focus = txt;
			
		}
		private function MOver (e:MouseEvent) :void {
			darkness.alpha = 1;
		}
		private function MOut (e:MouseEvent) :void {
			
				darkness.alpha = 0;
		}
		private function DDItemOver (e:MouseEvent) :void {
			var tar:MovieClip = e.currentTarget as MovieClip;
			tar.darkness.visible = true;
		}
		private function DDItemOut (e:MouseEvent) :void {
			var tar:MovieClip = e.currentTarget as MovieClip;
				tar.darkness.visible = false;
		}
		private function Tselect (e:Event) :void {
			selected = true;
			txt.textColor = 0xFFFFFF;
			trace("select")
			addEventListener(Event.ENTER_FRAME, removeTxt, false, 0, true);
		}
		private function Tdeselect (e:Event) :void {
			setText();
		}
		private function removeTxt(e:Event) :void {
			var str:String = txt.text;
			if (txt.text.length !=0) {
				txt.text = str.substr(0,str.length-2);
			} else {
				removeEventListener(Event.ENTER_FRAME, removeTxt, false);
				Tchange();
			}
		}
		private function Tchange (e:Event = null) :void {
			if(!stopOnChange) {
				highlightedDDItem = -1;
				dispatchEvent( new EventWithParams("UPDATE", false, false, [txt.text]));
			} else {
				stopOnChange = false;
			}
		}
		public function newDropDownList($list:Vector.<String>) :void {
			removedCount = 0;
			dropDownList.forEach(removeOldDDItems);
			this.dropDownList.length = 0;
			
			
			for (var i:int = 0; i < $list.length; i++) {
				dropDnItem = new dropDownItem();
				dropDnItem.txt.text = $list[i];
				dropDnItem.darkness.visible = false;
				dropDnItem.buttonMode = true;
				dropDnItem.useHandCursor = true;
				dropDnItem.txt.mouseEnabled = false;
				dropDnItem.darkness.mouseEnabled = false;
				dropDnItem.y = (dropDnItem.height + 2)*i+34;
				addChild(dropDnItem);
				dropDnItem.addEventListener(MouseEvent.CLICK, clickDDItem, false, 0, true);
				dropDnItem.addEventListener(MouseEvent.ROLL_OVER, DDItemOver, false, 0, true);
				dropDnItem.addEventListener(MouseEvent.ROLL_OUT, DDItemOut, false, 0, true);
				dropDownList.push(dropDnItem);
			}
			if(removedCount==0){
				for (var j:int = 0; j < $list.length; j++) {
					//dropDownList[j].alpha = 1;
					TweenLite.from(dropDownList[j], .3,{ease:Quad.easeInOut, y:0, alpha:0});
				}
			highlightedDDItem = -1;
			}
		} 
		
		private function clickDDItem (e:MouseEvent) :void {
			var tar:MovieClip = e.target.parent as MovieClip;
			txt.text = "";
			txt.text = tar.txt.text;
			clearDD();
			highlightedDDItem = -1;
			dispatchEvent( new Event ("NEW_SELECTION"));
		}
		private function clearDD ():void {
			for(var i = 0; i<dropDownList.length; i++) {
				TweenLite.to(dropDownList[i], .3,{ease:Quad.easeInOut, y:0, alpha:0, onComplete:killMe, onCompleteParams:[dropDownList[i]]});
			}
			
		}
		
		private function killMe($tar:MovieClip) :void {
			removeChild($tar);
			dropDownList.length = 0;
		}
		private function removeOldDDItems (mc:MovieClip, index:int, Vect:Vector.<MovieClip>) {
				removeChild(mc);
				removedCount++;
			}
		
		public function setText(e:Event = null, $newTxt:String = ""):void {
			if ($newTxt != "")
				txt.text = $newTxt;
			
			txt.textColor = 0x000000;
			stage.focus = null;
			darkness.alpha = 0;
			stage.removeEventListener(MouseEvent.CLICK, setText, false);
		}
		
		
	}
}